package com.lashou.chunshen.lashoutuangou.core;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;

import com.lashou.chunshen.lashoutuangou.R;
import com.lashou.chunshen.lashoutuangou.tools.MyActivityManager;
import com.lashou.chunshen.lashoutuangou.widget.ToolBarHelper;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/8/30.
 */

public abstract class BaseActivity extends AppCompatActivity {
    private ToolBarHelper mToolBarHelper;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyActivityManager.getAppManager().addActivity(this);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        if(isNeedTitleBar()){
            mToolBarHelper = new ToolBarHelper(this,layoutResID);
            Toolbar toolbar = mToolBarHelper.getToolBar();
            setContentView(mToolBarHelper.getContentView());
            setSupportActionBar(toolbar);
            onCreateCustomToolBar(toolbar);
        }else{
          //  setContentView(getContentViewId());
           setContentView(LayoutInflater.from(this).inflate(getContentViewId(),null));
        }
        ButterKnife.inject(this);
    }
    private void onCreateCustomToolBar(Toolbar toolbar) {

    }

    //获取第一个fragment
    protected abstract BaseFragment getFirstFragment();

    //获取Intent
    protected void handleIntent(Intent intent) {

    }
    //布局文件ID
    protected abstract int getContentViewId();

    //布局中Fragment的ID
    protected abstract int getFragmentContentId();

    /**子类实现是否需要titilebar**/
    protected abstract boolean isNeedTitleBar();

    //添加fragment
    protected void addFragment(BaseFragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(getFragmentContentId(), fragment, fragment.getClass().getSimpleName())
                    .addToBackStack(fragment.getClass().getSimpleName())
                    .commitAllowingStateLoss();
        }
    }

    //移除fragment
    protected void removeFragment() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            getSupportFragmentManager().popBackStack();
        } else {
            finish();
        }
    }
    //返回键返回事件
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
                finish();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.animator.anmi_in_left_right, R.animator.anmi_out_left_right);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.animator.anmi_in_left_right, R.animator.anmi_out_left_right);
    }
    //点击空白位置 隐藏软键盘
    public boolean onTouchEvent(MotionEvent event) {
        if (null != this.getCurrentFocus()) {
            /**
             * 点击空白位置 隐藏软键盘
             */
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this
                    .getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyActivityManager.getAppManager().addActivity(this);
    }
}
